package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Program: health_parent
 * @Description:
 * @Author: choulan
 * @Date:2020-11-23 10:34
 **/
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private UserDao userDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取到分页参数
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页插件
        PageHelper.startPage(currentPage,pageSize);
        //将查询到的数据封装到Page对象中
        Page<Role> page= roleDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Role> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public List<Permission> findAuthority() {
        return permissionDao.findAuthority();
    }

    @Override
    public List<Menu> findMenu() {
        return menuDao.findMenu();
    }

    @Override
    public void add(Role role, Integer[] authorityIds, Integer[] menuIds) {
        //添加一个role
        roleDao.add(role);
        Integer roleId = role.getId();
        //添加role_permission关联关系
        this.setRolePermissonAssociation(roleId,authorityIds);
        //添加role_menu关联关系
        this.setRoleMenuAssociation(roleId,menuIds);
    }

    @Override
    public Role findById(Integer id) {
        //查找角色基本信息
        return roleDao.findByRoleId(id);

    }

    @Override
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        LinkedHashSet<Menu> menus = menuDao.findByRoleId(id);
        List<Integer> list=new ArrayList<>();
        if(menus!=null&&menus.size()>0) {
            for (Menu menu : menus) {
                list.add(menu.getId());
            }
        }
        return list;
    }

    @Override
    public List<Integer> findAuthorityIdsByRoleId(Integer id) {
        Set<Permission> permissions = permissionDao.findByRoleId(id);
        List<Integer> list=new ArrayList<>();
        if(permissions!=null&&permissions.size()>0) {
            for (Permission permission : permissions) {
                list.add(permission.getId());
            }
        }
        return list;
    }

    @Override
    public void edit(Role role, Integer[] authorityIds, Integer[] menuIds) {
        //编辑角色信息
        roleDao.edit(role);
        Integer roleId = role.getId();
        //清除角色与权限的关联
        roleDao.deleteRolePermissionAssociation(roleId);
        //清除角色与菜单的关联
        roleDao.deleteRoleMenuAssociation(roleId);
        //新建角色与权限的关联
        this.setRolePermissonAssociation(roleId,authorityIds);
        //新建角色与菜单的关联
        this.setRoleMenuAssociation(roleId,menuIds);
    }

    @Override
    public void delete(Integer id) {
        //清除角色与权限的关联
        roleDao.deleteRolePermissionAssociation(id);
        //清除角色与菜单的关联
        roleDao.deleteRoleMenuAssociation(id);

        //通过roleId查找userId
        Integer userId= roleDao.findUserIdByRoleId(id);
        //清除角色与用户的关联
        userDao.deleteUserRoleAssociation(userId);
        //删除角色
        roleDao.delete(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public boolean checkName(String name) {
       Integer i= roleDao.checkName(name);
       if(i==0){
           return false;
       }
        return true;
    }

    public void setRoleMenuAssociation(Integer roleId, Integer[] menuIds) {
        if(menuIds!=null&&menuIds.length>0){
            for (Integer menuId : menuIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                roleDao.setRoleMenuAssociation(map);
            }
        }
    }

    public void setRolePermissonAssociation(Integer roleId, Integer[] authorityIds) {

        if(authorityIds!=null&&authorityIds.length>0){
            for (Integer authorityId : authorityIds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",authorityId);
                roleDao.setRolePermissonAssociation(map);
            }
        }
    }


}
